package com.zch.rpc.core.invoke;

import com.zch.rpc.core.model.RpcRequestModel;
import com.zch.rpc.core.model.RpcResponseModel;
import com.zch.rpc.core.net.abstracts.Client;
import com.zch.rpc.core.net.jetty1.JettyClient;
import com.zch.rpc.core.register.RegisterService;
import com.zch.rpc.core.serialize.Serializer;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.UUID;

/**
 * @Auther: zch
 * @Date: 2018/12/28 17:14
 * @Description:
 */
public class RpcProxyHandler implements InvocationHandler {
    private Class<?> interfaceClass;
    private  Serializer serializer;
    private  String serverIpPort;
    private  Class<?extends RegisterService> registerService;
    private  String version;
    private  String groupId;
    private JettyClient client;

    public RpcProxyHandler(Class<?> interfaceClass, Serializer serializer, String serverIpPort, Class<? extends RegisterService> registerService, String version, String groupId) {
        this.interfaceClass = interfaceClass;
        this.serializer = serializer;
        this.serverIpPort = serverIpPort;
        this.registerService = registerService;
        this.version = version;
        this.groupId = groupId;
        initClient();
    }
    private void initClient(){
        this.client=new JettyClient();
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        RpcRequestModel requestModel=new RpcRequestModel();
        requestModel.setRequestId(UUID.randomUUID().toString());
        requestModel.setVersion(version);
        requestModel.setGroupId(groupId);
        requestModel.setParams(args);
        requestModel.setMethodName(method.getName());
        requestModel.setParamTypes(method.getParameterTypes());
        requestModel.setInterfaceClass(interfaceClass);
        RpcResponseModel responseModel=client.send(requestModel,serverIpPort);
        if(responseModel.getData()!=null){
            return responseModel.getData();
        }
        return null;
    }


    public Serializer getSerializer() {
        return serializer;
    }

    public void setSerializer(Serializer serializer) {
        this.serializer = serializer;
    }

    public String getServerIpPort() {
        return serverIpPort;
    }

    public void setServerIpPort(String serverIpPort) {
        this.serverIpPort = serverIpPort;
    }

    public Class<? extends RegisterService> getRegisterService() {
        return registerService;
    }

    public void setRegisterService(Class<? extends RegisterService> registerService) {
        this.registerService = registerService;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getGroupId() {
        return groupId;
    }

    public void setGroupId(String groupId) {
        this.groupId = groupId;
    }

    public Class<?> getInterfaceClass() {
        return interfaceClass;
    }

    public void setInterfaceClass(Class<?> interfaceClass) {
        this.interfaceClass = interfaceClass;
    }

    public JettyClient getClient() {
        return client;
    }

    public void setClient(JettyClient client) {
        this.client = client;
    }
}
